#ifndef PLOTAPI
#define PLOTAPI

#include <highgui.h>
#include <cv.h>
#include <cvaux.h>

using namespace std;
//using namespace cv;

const CvScalar RED = cvScalar(0,0,255);
const CvScalar PINK = cvScalar(230,130,255);
const CvScalar BLUE = cvScalar(255,0,0);
const CvScalar LIGHTBLUE = cvScalar(255,255,160);
const CvScalar GREEN = cvScalar(0,255,0);

CvScalar hsv2rgb( float hue )
{
    int rgb[3], p, sector;
    static const int sector_data[][3]=
        {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
    hue *= 0.033333333333333333333333333333333f;
    sector = cvFloor(hue);
    p = cvRound(255*(hue - sector));
    p ^= sector & 1 ? 255 : 0;

    rgb[sector_data[sector][0]] = 255;
    rgb[sector_data[sector][1]] = 0;
    rgb[sector_data[sector][2]] = p;

    return cvScalar(rgb[2], rgb[1], rgb[0],0);
}

void draw_cross(CvScalar color,int  d, IplImage *image, int x_pos, int y_pos)
{
  cvLine( image, cvPoint( x_pos - d, y_pos - d ),			\
	  cvPoint( x_pos + d, y_pos + d ), color,1, 0 );
  cvLine( image, cvPoint( x_pos + d, y_pos - d ),			\
	  cvPoint(  x_pos - d,  y_pos + d ), color, 1, 0 );
}

void draw_arrow(CvScalar color, int x, int y, IplImage *image, int x_pos, int y_pos)
{
  cvLine( image, cvPoint(x_pos-y,y_pos+x),				\
	  cvPoint( x_pos+x*5-y, y_pos+y*5+x ), color,1, 0 );
  cvLine( image, cvPoint( x_pos+x*5-y, y_pos+y*5+x ),			\
	  cvPoint(  x_pos+x*5-y*2, y_pos+y*5+x*2 ), color,1, 0 );
  cvLine( image, cvPoint(  x_pos+x*5-y*2, y_pos+y*5+x*2 ),		\
	  cvPoint( x_pos+x*7, y_pos+y*7 ), color,1, 0 );
  cvLine( image, cvPoint(x_pos+y,y_pos-x),				\
	  cvPoint(  x_pos+x*5+y, y_pos+y*5-x ), color,1, 0 );
  cvLine( image, cvPoint( x_pos+x*5+y, y_pos+y*5-x ),			\
	  cvPoint(  x_pos+x*5+y*2, y_pos+y*5-x*2 ), color,1, 0 );
  cvLine( image, cvPoint(  x_pos+x*5+y*2, y_pos+y*5-x*2 ),		\
	  cvPoint( x_pos+x*7, y_pos+y*7 ), color,1, 0 );
  cvLine( image, cvPoint(x_pos-y,y_pos+x),			\
	  cvPoint(x_pos+y,y_pos-x), color,1, 0 );
}

void draw_rectangle(CvScalar color,int  d_x, int d_y, IplImage *image, int x_pos, int y_pos)
{
  cvLine( image, cvPoint( x_pos - d_x, y_pos + d_y ),			\
	  cvPoint( x_pos + d_x, y_pos + d_y ), color,1, 0 );
  cvLine( image, cvPoint( x_pos + d_x, y_pos + d_y ),			\
	  cvPoint(  x_pos + d_x,  y_pos - d_y ), color, 1, 0 );
  cvLine( image, cvPoint( x_pos + d_x, y_pos - d_y ),			\
	  cvPoint(  x_pos - d_x,  y_pos - d_y ), color, 1, 0 );
  cvLine( image, cvPoint( x_pos - d_x, y_pos - d_y ),			\
	  cvPoint(  x_pos - d_x,  y_pos + d_y ), color, 1, 0 );
}
 
void draw_square(CvScalar color,int  d, IplImage *image, int x_pos, int y_pos)
{
  cvLine( image, cvPoint( x_pos - d, y_pos + d ),		\
	  cvPoint( x_pos + d, y_pos + d ), color,3, 0 );
  cvLine( image, cvPoint( x_pos + d, y_pos + d ),		\
	  cvPoint(  x_pos + d,  y_pos - d ), color, 3, 0 );
  cvLine( image, cvPoint( x_pos + d, y_pos - d ),			\
	  cvPoint(  x_pos - d,  y_pos - d ), color, 3, 0 );
  cvLine( image, cvPoint( x_pos - d, y_pos - d ),			\
	  cvPoint(  x_pos - d,  y_pos + d ), color, 3, 0 );

  cvLine( image, cvPoint( x_pos + 5, y_pos ),			\
	  cvPoint(  x_pos - 5,  y_pos  ), color, 2, 0 );
  cvLine( image, cvPoint( x_pos , y_pos - 5 ),			\
	  cvPoint(  x_pos ,  y_pos + 5 ), color, 2, 0 ); 

}

void plot_box (CvBox2D track_box, IplImage* src,float hue)
{
  CvPoint2D32f box_vtx[4];
  CvPoint pt0, pt;
  
  cvBoxPoints( track_box, box_vtx );

  CvScalar color= hsv2rgb(hue);

  pt0.x = cvRound(box_vtx[3].x);
  pt0.y = cvRound(box_vtx[3].y);
  
  for( int i = 0; i < 4; i++ )
    {
      pt.x = cvRound(box_vtx[i].x);
      pt.y = cvRound(box_vtx[i].y);
      

      cvLine(src, pt0, pt, color, 4, CV_AA, 0);
      pt0 = pt;
    }
}

void plot_bar (IplImage* src,int index,char* name, int percentage)
{
  // Init font
  CvFont font;
  cvInitFont(&font,CV_FONT_VECTOR0,0.6,0.6,0,2);

  cvRectangle (src, cvPoint(src->width-20*(1+index),src->height-10),cvPoint(src->width-(10+index*20), 50),RED,2,8,0);
  cvRectangle (src, cvPoint(src->width-20*(1+index),(src->height-60)/2+50),cvPoint(src->width-(10+index*20),(src->height-60)/2+50-percentage*(src->height-60)/200),GREEN,CV_FILLED,8,0);
  cvPutText (src,name,cvPoint(src->width-20*(1+index),30),&font,RED);
}  

void plot_text (IplImage* src,char* name,int x, int y)
{
  // Init font
  CvFont font;
  cvInitFont(&font,CV_FONT_VECTOR0,0.6,0.6,0,2);

  cvPutText (src,name,cvPoint(x,y),&font,RED);
}  

#endif
